//
// Created by liushuai on 2021/1/26.
//

#include "replaceSpace.h"
<<<<<<< HEAD:cxx/algorithm/src/replaceSpace.cc
string replaceSpace(string s){
    int space_count = 0;
    string replaceStr = "%20";
    int last_elem = s.size();
    for(auto e:s){
        if(e==' ')
            space_count++;
    }
    s.resize(s.size()+2*(space_count)+1);
    int current_index = s.size()-1;
    while(last_elem!=current_index&&current_index>=0&&last_elem>=0){
        if(s[last_elem]!=' '){
            s[current_index] = s[last_elem];
            last_elem--;
            current_index--;
        }else{
            for(int i=replaceStr.size()-1;i>=0;i--){
                s[current_index]=replaceStr[i];
                current_index--;
            }
            last_elem--;
        }
    }
    return s;

=======
string replaceSpace(string s) {
  int space_count = 0;
  string replaceStr = "%20";
  int last_elem = s.size();
  for (auto e : s) {
    if (e == ' ') space_count++;
  }
  s.resize(s.size() + 2 * (space_count) + 1);
  int current_index = s.size() - 1;
  while (last_elem != current_index && current_index >= 0 && last_elem >= 0) {
    if (s[last_elem] != ' ') {
      s[current_index] = s[last_elem];
      last_elem--;
      current_index--;
    } else {
      for (int i = replaceStr.size() - 1; i >= 0; i--) {
        s[current_index] = replaceStr[i];
        current_index--;
      }
      last_elem--;
    }
  }
  return s;
>>>>>>> dev:cxx/algorithm/common/src/replaceSpace.cc
}